﻿Imports DTO
Imports BUS
Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors
Public Class frmThemNhanVien

    Public Delegate Sub ThemNV(nv As NhanVienDTO)

    Public Event LamMoiNVKhiThem As ThemNV

    Public kt As Integer = 0

    Public maNV As String
    Public tenNV As String
    Public diaChi As String
    Public dienThoai As String
    Public diDong As String
    Public email As String
    Public ghiChu As String
    Public maBoPhan As String
    Public maNhanVienQL As String
    Public maTT As Integer


    Private Sub ComboBoxEdit2_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxQuanLyNV.Properties.ButtonClick
        If e.Button.Tag.ToString() = "cbxbtnQuanLy" Then
            Dim frm As New frmThemNhanVien
            frm.ShowDialog()
        End If
    End Sub

    Private Sub cbxBoPhanNV_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxBoPhanNV.Properties.ButtonClick
        If e.Button.Tag.ToString() = "cbxbtnBoPhan" Then
            Dim frm As New frmThemQuanLy
            frm.ShowDialog()
        End If
    End Sub

    Private Sub LoadCbxBoPhan()
        cbxBoPhanNV.Properties.DataSource = BoPhanBUS.LayDanhSachBoPhanCBX()
        cbxBoPhanNV.Properties.ValueMember = "MaBoPhan"
        cbxBoPhanNV.Properties.DisplayMember = "TenBoPhan"
        SetEditValueByIndex(cbxBoPhanNV, 0)
    End Sub

    'hàm có chức năng selectedindex = 0
    Private Sub SetEditValueByIndex(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxBoPhanNV.Properties.GetKeyValue(index)
        cbxBoPhanNV.EditValue = keyValue
    End Sub

    Private Sub LoadCbxNhanVienQL()
        cbxQuanLyNV.Properties.DataSource = NhanVienBUS.LayDSNhanVienCBX()
        cbxQuanLyNV.Properties.ValueMember = "MaNhanVien"
        cbxQuanLyNV.Properties.DisplayMember = "TenNhanVien"

    End Sub

    Private Sub frmThemNhanVien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadCbxBoPhan()
        LoadCbxNhanVienQL()
        If kt = 1 Then

            txtThemMaNV.Text = maNV
            txtThemTenNV.Text = tenNV
            txtThemDienThoaiNV.Text = dienThoai
            txtThemGhiChu.Text = ghiChu
            txtThemDiaChiNV.Text = diaChi
            txtEmailNV.Text = email
            txtThemDiDongNV.Text = diDong
            If maTT = 1 Then
                chkThemQuanLyNV.Checked = True
            Else
                chkThemQuanLyNV.Checked = False
            End If
            NhanVienBUS.LayMaBoPhanCbx(maNV, maBoPhan, maNhanVienQL)
            cbxBoPhanNV.EditValue = maBoPhan
            cbxQuanLyNV.EditValue = maNhanVienQL
        End If

        
    End Sub

    Private Sub frmThemKH_btnDongKhoHang_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnDongKhoHang.Click
        NhanVienBUS.DongKetNoi.Close()
        Me.Close()
    End Sub

    Private Sub ThemNhanVien()
        NhanVienBUS.DongKetNoi.Close()
        Dim nhanvien As New NhanVienDTO
        nhanvien.TenNhanVien = txtThemTenNV.Text.Trim()
        nhanvien.DienThoai = txtThemDienThoaiNV.Text.Trim()
        nhanvien.Email = txtEmailNV.Text.Trim()
        nhanvien.DiaChi = txtThemDiaChiNV.Text.Trim()
        nhanvien.GhiChu = txtThemGhiChu.Text.Trim()
        nhanvien.DiDong = txtThemDiDongNV.Text.Trim()
        If chkThemQuanLyNV.Checked = True Then
            nhanvien.MaTrangThai = 1
        Else
            nhanvien.MaTrangThai = 0
        End If
        nhanvien.MaBoPhan = cbxBoPhanNV.EditValue.ToString()
        If cbxQuanLyNV.Text.Trim() = "" Then
            nhanvien.MaNguoiQL = Nothing
        Else
            nhanvien.MaNguoiQL = cbxQuanLyNV.EditValue.ToString()
        End If
        If NhanVienBUS.ThemNhanVien(nhanvien) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm thành công nhân viên", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Không được để trống tên nhân viên", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        RaiseEvent LamMoiNVKhiThem(nhanvien)

    End Sub

    Private Sub SuaNhanVien()
        NhanVienBUS.DongKetNoi.Close()
        Dim nhanvien As New NhanVienDTO
        nhanvien.MaNhanVien = txtThemMaNV.Text.Trim()
        nhanvien.TenNhanVien = txtThemTenNV.Text.Trim()
        nhanvien.DienThoai = txtThemDienThoaiNV.Text.Trim()
        nhanvien.Email = txtEmailNV.Text.Trim()
        nhanvien.DiaChi = txtThemDiaChiNV.Text.Trim()
        nhanvien.GhiChu = txtThemGhiChu.Text.Trim()
        nhanvien.DiDong = txtThemDiDongNV.Text.Trim()
        If chkThemQuanLyNV.Checked = True Then
            nhanvien.MaTrangThai = 1
        Else
            nhanvien.MaTrangThai = 0
        End If
        nhanvien.MaBoPhan = cbxBoPhanNV.EditValue.ToString()
        If cbxQuanLyNV.Text.Trim() = "" Then
            nhanvien.MaNguoiQL = Nothing
        Else
            nhanvien.MaNguoiQL = cbxQuanLyNV.EditValue.ToString()
        End If
        If NhanVienBUS.CapNhatNhanVien(nhanvien) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Cập nhập thành công nhân viên", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Đã có lỗi xảy ra", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub frmThemKH_btnThemKhoHang_Click(sender As Object, e As EventArgs) Handles frmThemKH_btnThemKhoHang.Click
        NhanVienBUS.DongKetNoi.Close()
        If kt = 1 Then
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 29)
            If pq.Sua = True Then
                SuaNhanVien()
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý nhân viên", "Cập nhật", maNV)
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 29)
            If pq.Them = True Then
                ThemNhanVien()
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý nhân viên", "Thêm", maNV)
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub
End Class